﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Globalization;
namespace QLDIAOC
{
    /// <summary>
    /// Interaction logic for DANG_NHAP.xaml
    /// </summary>
    public partial class DANG_NHAP : Window
    {
        public SqlConnection m_SCN;
        SqlCommand mySqlCommand;
        SqlDataReader mySqlDataReader;
        public DANG_NHAP()
        {
            m_SCN = new SqlConnection();
            try
            {
                // Open the connection
                string path = System.IO.Directory.GetCurrentDirectory();

                m_SCN.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + path +
                    @"\data\QLDIAOC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                m_SCN.Open();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
                MessageBox.Show("Lỗi kết nối CSDL", "ERROR!");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            mySqlCommand = new SqlCommand("DANGNHAP", m_SCN);
            mySqlCommand.CommandType = CommandType.StoredProcedure;
            mySqlCommand.Parameters.Add(new SqlParameter("@TenDangNhap", SqlDbType.VarChar, 50));
            mySqlCommand.Parameters["@TenDangNhap"].Value = textBox1.Text;
            mySqlDataReader = mySqlCommand.ExecuteReader();
            if (mySqlDataReader.Read())
            {
                if (mySqlDataReader["MaKhau"].ToString() == passwordBox1.Password)
                {
                    mySqlCommand.Dispose();
                    mySqlDataReader.Dispose();
                    MainWindow m_Main = new MainWindow(m_SCN);
                    m_Main.Show();

                    this.Close();
                }
                else
                {
                    m_txblMatKhau.Text = "Mật khẩu sai";
                }
            }
            else
            {
                m_txblID.Text = "Tên đăng nhập không tồn tại";


            }
            mySqlDataReader.Dispose();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_txblID.Text = "";
        }

        private void passwordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {
            m_txblMatKhau.Text = "";
        }
    }
}
